package secondriver.heathfood.common;

import android.os.Bundle;
import android.os.Message;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author : secondriver
 * Date :  2015/10/22
 */
public class BackgroundTaskExecutor {

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {

        private final AtomicInteger COUNTER = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "BackgroundTask #" + COUNTER.getAndIncrement());
            return t;
        }
    };

    private static final ExecutorService executor = Executors.newFixedThreadPool(3, sThreadFactory);

    public static void doTask(final Bundle bundle, final Message message, final BackgroundTask task) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                task.executeTask(bundle, message);
            }
        };
        executor.execute(r);
    }

    public interface BackgroundTask {
        void executeTask(final Bundle bundle, final Message message);
    }

}